import java.io.*;
import java.util.Scanner;

public class text {

    private static String newStr = "xiaoming";
    private static String oldStr = "ming";
    private static String newnum = "    ";
    private static String oldnum = "9999";
    private static long pos;
    private static String content;

    public static void readTxt(String filePath) {

        try {
            File file = new File(filePath);
            if (file.isFile() && file.exists()) {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
                BufferedReader br = new BufferedReader(isr);
                String lineTxt;
                while ((lineTxt = br.readLine()) != null) {
                    System.out.println(lineTxt);
                }
                br.close();
            } else {
                System.out.println("文件不存在");
            }
        } catch (Exception e) {
            System.out.println("文件读取错误");
        }

    }

    public static void insert(long pos, String content) {
        File tempFile;  //生成临时文件
        try {

            tempFile = File.createTempFile("C:\\Users\\HUAWEI\\Desktop\\information.txt", "txt");
            tempFile.deleteOnExit();
            FileOutputStream fos = new FileOutputStream(tempFile);
            File filePath = null;    //操作路径
            RandomAccessFile raf = new RandomAccessFile("C:\\Users\\HUAWEI\\Desktop\\information.txt", "rw");  //读取文件内容
            raf.seek(2);//指针，指到第几个字
            byte[] buffer = new byte[15];
            int num = 0;
            while (-1 != raf.read(buffer)) {
                fos.write(buffer, 0, 11); //将指定字节输入
            }
            raf.seek(2);
            raf.writeBytes("133****1234");//替换并写入
            FileInputStream fis = new FileInputStream(filePath); //字节输入流
            while (-1 != fis.read(buffer)) {
                raf.write(buffer, 0, 11);

            }
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void change(String oldStr, String newStr) {

        try {

            RandomAccessFile raf = new RandomAccessFile("C:\\Users\\HUAWEI\\Desktop\\information.txt", "rw");//读取文件
            String line;
            while (null != (line = raf.readLine())) {
                if (line.contains(oldStr)) {
                    String[] split = line.split(oldStr);
                    raf.seek(split[0].length());
                    raf.writeBytes(newStr);
                    raf.writeBytes(split[1]);
                }
            }
            raf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void delete(String oldnum, String newnum) {

        try {

            RandomAccessFile raf = new RandomAccessFile("C:\\Users\\HUAWEI\\Desktop\\information.txt", "rw");//读取文件
            String line;
            while (null != (line = raf.readLine())) {
                if (line.contains(oldnum)) {
                    String[] split = line.split(oldnum);
                    raf.seek(split[0].length());
                    raf.writeBytes(newnum);
                    raf.writeBytes(split[1]);
                }
            }
            raf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println("账户信息如下：");
        String filePath = "C:\\Users\\HUAWEI\\Desktop\\information.txt";
        readTxt(filePath);
        System.out.println("增加了手机号，修改了姓名，删掉余额");
        System.out.println("能力有限，只有一条账户信息（1）");
        System.out.println("请输入账户ID");
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        if (a == 1) {
            System.out.println("虽然报错了，但是运行成功了，请看TXT文件");
            delete(oldnum, newnum);
            change(oldStr, newStr);
            insert(pos, content);
        }

    }
}







